﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class QuanLyTuyen : Form
    {
        public QuanLyTuyen()
        {
            InitializeComponent();
        }
        public void ShowMaster()
        {
            this.dgvTuyen.AutoGenerateColumns = true;
            this.dgvTuyen.DataSource = TUYENDL_BUS.SelectTuyenDuLichsAll();
        }
        public void ShowMasterThanhPho()
        {
            this.dgvDanhSachThanhPho.AutoGenerateColumns = true;
            //String maTuyen = tbMaTuyen.Text;
            //int maTuyen = int.Parse(tbMaTuyen.Text);
            // maTuyen = 0;//Khoi tao
            this.dgvDanhSachThanhPho.DataSource = TP_TRUNGGIAN_BUS.SelectThanhPhoTrungGiansAll();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btThemTuyen_Click(object sender, EventArgs e)
        {
            TUYENDL_DTO tdl = new TUYENDL_DTO();
            //tdl.MaTuyen = int.Parse(tbMaTuyen.ToString());
            //tdl.TenTuyen.Text = tbTenTuyen.ToString();
            tdl.TenTuyenDL = tbTenTuyen.Text;
            //tdl.MaTP = int.Parse(cbThanhPho.SelectedValue.ToString());
            tdl.ThanhPhoDi = int.Parse(comboBox1.SelectedValue.ToString());
            tdl.ThanhPhoDen=int.Parse(comboBox2.SelectedValue.ToString());
            bool result = TUYENDL_BUS.ThemTuyenDuLich(tdl);
            int maTuyen = TUYENDL_BUS.SelectTuyenDuLichByTen(tbTenTuyen.Text);
            //tbMaTuyen.Text = tdl.MaTuyen.ToString();
            //tbMaTuyen.Text = int.Parse(TuyenDuLichBUS.SelectTuyenDuLichByTen(tbTenTuyen).ToString());
            tdl.MaTuyenDL = maTuyen;
            tbMaTuyen.Text = tdl.MaTuyenDL.ToString();
            if (result == true)
            {
                MessageBox.Show("Successful!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ShowMaster();

            }
            else
            {
                MessageBox.Show("Fail!\r\nChoose new TuyenDuLichID,Please!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void QuanLyTuyen_Load(object sender, EventArgs e)
        {
            this.ShowMaster();
            this.ShowMasterThanhPho();
            THANHPHO_BUS tp = new THANHPHO_BUS();

            cbThanhPho.DataSource = THANHPHO_BUS.SelectThanhPhosAll();
            cbThanhPho.ValueMember = "MaTP";
            cbThanhPho.DisplayMember = "TenTP";
            comboBox1.DataSource = THANHPHO_BUS.SelectThanhPhosAll();
            comboBox1.ValueMember = "MaTP";
            comboBox1.DisplayMember = "TenTP";
            comboBox2.DataSource = THANHPHO_BUS.SelectThanhPhosAll();
            comboBox2.ValueMember = "MaTP";
            comboBox2.DisplayMember = "TenTP";
            
        }

        private void btThemThanhPho_Click(object sender, EventArgs e)
        {
            TP_TRUNGGIAN_DTO tptg = new TP_TRUNGGIAN_DTO();
            
            if(tbMaTuyen.Text != "")
                {
                    tptg.MaTuyenDL = int.Parse(tbMaTuyen.Text);
                    tptg.MaTP = int.Parse(cbThanhPho.SelectedValue.ToString());
                    bool result = TP_TRUNGGIAN_BUS.ThemThanhPhoTrungGian(tptg);
                    if (result == true)
                    {
                        MessageBox.Show("Successful!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.ShowMasterThanhPho();
                    }
                    else
                    {
                        MessageBox.Show("Fail!\r\nChoose new TuyenDuLichID,Please!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            else
                {
                    MessageBox.Show("Fail!\r\n Hãy chọn tuyến lại!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            this.tbMaTuyen.Text = this.dgvDanhSachThanhPho.CurrentRow.Cells["staticMaTuyenDL"].Value.ToString();
            int matuyen = int.Parse(this.tbMaTuyen.Text.ToString());
            this.dgvDanhSachThanhPho.DataSource = TP_TRUNGGIAN_BUS.SelectThanhPhoTrungGiansByAndMaTuyen(matuyen);
        }

        private void dgvTuyen_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            THANHPHO_BUS tp = new THANHPHO_BUS();
            THANHPHO_DTO tp1 = new THANHPHO_DTO();
            THANHPHO_DTO tp2 = new THANHPHO_DTO();
            if (this.dgvTuyen.CurrentRow != null)
            {
                this.tbMaTuyen.Text = this.dgvTuyen.CurrentRow.Cells["MaTuyenDL"].Value.ToString();
                this.tbTenTuyen.Text = this.dgvTuyen.CurrentRow.Cells["TenTuyenDL"].Value.ToString();
                int matuyen = int.Parse(this.tbMaTuyen.Text.ToString());
                string text1 = this.dgvTuyen.CurrentRow.Cells["ThanhPhoDi"].Value.ToString();
                string text2 = this.dgvTuyen.CurrentRow.Cells["ThanhPhoDen"].Value.ToString();
                int MaTP1 = int.Parse(text1.ToString());
                int MaTP2 = int.Parse(text2.ToString());
                tp1 = THANHPHO_BUS.SelectThanhPhosByAndMaTP(MaTP1);
                tp2 = THANHPHO_BUS.SelectThanhPhosByAndMaTP(MaTP2);
                this.comboBox1.Text = tp1.TenTP;
                this.comboBox2.Text = tp2.TenTP;
                this.dgvDanhSachThanhPho.DataSource = TP_TRUNGGIAN_BUS.SelectThanhPhoTrungGiansByAndMaTuyen(matuyen);
            }
        }

        private void btSuaTuyen_Click(object sender, EventArgs e)
        {
            TUYENDL_DTO tdl = new TUYENDL_DTO();

            tdl.MaTuyenDL = int.Parse(tbMaTuyen.Text);
            tdl.TenTuyenDL = tbTenTuyen.Text;
            tdl.ThanhPhoDi = int.Parse(comboBox1.SelectedValue.ToString());
            tdl.ThanhPhoDen = int.Parse(comboBox2.SelectedValue.ToString());
            bool result = TUYENDL_BUS.SuaTuyenDuLich(tdl);
            if (result == true)
            {
                MessageBox.Show("Successful!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ShowMaster();
            }
            else
            {
                MessageBox.Show("Fail!\r\nCheck EmployeeID,Please!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btXoaTuyen_Click(object sender, EventArgs e)
        {
            if (this.dgvTuyen.CurrentRow != null)
            {
                String maTuyen = this.dgvTuyen.CurrentRow.Cells["MaTuyenDL"].Value.ToString();
                int MaTuyen = int.Parse(maTuyen.ToString());

                DialogResult dr = MessageBox.Show("Do you want to delete MaTuyen " + maTuyen + "?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dr == DialogResult.Yes)
                {
                    bool result = TUYENDL_BUS.XoaTuyenDuLich(maTuyen);
                    if (result == true)
                    {
                        MessageBox.Show("Successful!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.ShowMaster();
                    }
                    else
                    {
                        MessageBox.Show("Fail!\r\nCheck EmployeeID,Please!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                }
            }
            else
            {
                MessageBox.Show("Fail!\r\nChoose one employee,Please!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn thoát ?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dr == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void dgvDanhSachThanhPho_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            THANHPHO_BUS tp = new THANHPHO_BUS();
            THANHPHO_DTO tp1 = new THANHPHO_DTO();
            THANHPHO_DTO tp2 = new THANHPHO_DTO();
            THANHPHO_DTO tp3 = new THANHPHO_DTO();
            TUYENDL_DTO tuyen = new TUYENDL_DTO();
            TUYENDL_BUS tuyenbus = new TUYENDL_BUS();
            if (this.dgvDanhSachThanhPho.CurrentRow != null)
            {
                this.tbMaTuyen.Text = this.dgvDanhSachThanhPho.CurrentRow.Cells["staticMaTuyenDL"].Value.ToString();
                string tptg = this.dgvDanhSachThanhPho.CurrentRow.Cells["MaTP"].Value.ToString();
                int matptg = int.Parse(tptg.ToString());
                int matuyen = int.Parse(tbMaTuyen.Text.ToString());
                tuyen = TUYENDL_BUS.SelectTuyenByMaTuyen(matuyen);
                this.tbTenTuyen.Text = tuyen.TenTuyenDL;

                int MaTP1 = int.Parse(tuyen.ThanhPhoDi.ToString());
                int MaTP2 = int.Parse(tuyen.ThanhPhoDen.ToString());
                tp1 = THANHPHO_BUS.SelectThanhPhosByAndMaTP(MaTP1);
                tp2 = THANHPHO_BUS.SelectThanhPhosByAndMaTP(MaTP2);
                tp3 = THANHPHO_BUS.SelectThanhPhosByAndMaTP(matptg);
                this.comboBox1.Text = tp1.TenTP;
                this.comboBox2.Text = tp2.TenTP;
                this.cbThanhPho.Text = tp3.TenTP;
            }
        }

        private void cbThanhPho_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.tbMaTuyen.Text = this.dgvDanhSachThanhPho.CurrentRow.Cells["staticMaTuyenDL"].Value.ToString();
            int matuyen = int.Parse(this.tbMaTuyen.Text.ToString());
            this.dgvDanhSachThanhPho.DataSource = TP_TRUNGGIAN_BUS.SelectThanhPhoTrungGiansByAndMaTuyen(matuyen);
        }
        private void btSuaThanhPho_Click(object sender, EventArgs e)
        {

            string sMaTuyen = this.dgvDanhSachThanhPho.CurrentRow.Cells["staticMaTuyenDL"].Value.ToString();
            string sMaTP1 = this.dgvDanhSachThanhPho.CurrentRow.Cells["MaTP"].Value.ToString();
            //string sMaTP2 = this.cbThanhPho.Text;
           // int iMaTP2 = int.Parse(sMaTP2.ToString());
            int iMaTP2 = int.Parse(cbThanhPho.SelectedValue.ToString());
            int iMaTP1 = int.Parse(sMaTP1.ToString());
            int iMaTuyen = int.Parse(sMaTuyen.ToString());
            bool result = TP_TRUNGGIAN_BUS.SuaThanhPhoTrungGian(iMaTuyen,iMaTP1,iMaTP2);
            if (result == true)
            {
                MessageBox.Show("Successful!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ShowMaster();
            }
            else
            {
                MessageBox.Show("Fail!\r\nCheck EmployeeID,Please!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.tbMaTuyen.Text = this.dgvDanhSachThanhPho.CurrentRow.Cells["staticMaTuyenDL"].Value.ToString();
            int matuyen = int.Parse(this.tbMaTuyen.Text.ToString());
            this.dgvDanhSachThanhPho.DataSource = TP_TRUNGGIAN_BUS.SelectThanhPhoTrungGiansByAndMaTuyen(matuyen);

            
        }

        private void btXoaThanhPho_Click(object sender, EventArgs e)
        {
            if (this.dgvDanhSachThanhPho.CurrentRow != null)
            {
                int maTuyen = int.Parse(dgvDanhSachThanhPho.CurrentRow.Cells["staticMaTuyenDL"].Value.ToString());
                int maTP = int.Parse(dgvDanhSachThanhPho.CurrentRow.Cells["MaTP"].Value.ToString());
                DialogResult dr = MessageBox.Show("Do you want to delete MaTP " + maTP + "?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dr == DialogResult.Yes)
                {
                    bool result = TP_TRUNGGIAN_BUS.XoaThanhPhoTrungGian(maTuyen, maTP);
                    if (result == true)
                    {
                        MessageBox.Show("Successful!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.ShowMasterThanhPho();
                    }
                    else
                    {
                        MessageBox.Show("Fail!\r\nCheck EmployeeID,Please!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                }
            }
            else
            {
                MessageBox.Show("Fail!\r\nChoose one employee,Please!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.tbMaTuyen.Text = this.dgvDanhSachThanhPho.CurrentRow.Cells["staticMaTuyenDL"].Value.ToString();
            int matuyen = int.Parse(this.tbMaTuyen.Text.ToString());
            this.dgvDanhSachThanhPho.DataSource = TP_TRUNGGIAN_BUS.SelectThanhPhoTrungGiansByAndMaTuyen(matuyen);
        }

        private void btThemTP_Click(object sender, EventArgs e)
        {
            ThemTP themTP = new ThemTP();
            themTP.ShowDialog();
            cbThanhPho.DataSource = THANHPHO_BUS.SelectThanhPhosAll();
            cbThanhPho.ValueMember = "MaTP";
            cbThanhPho.DisplayMember = "TenTP";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ThemTP themTP = new ThemTP();
            themTP.ShowDialog();
            cbThanhPho.DataSource = THANHPHO_BUS.SelectThanhPhosAll();
            cbThanhPho.ValueMember = "MaTP";
            cbThanhPho.DisplayMember = "TenTP";
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
    }
}
